package android.tkb.activity.word;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.tkb.dao.word.DatabaseHelper;
import android.tkb.model.word.Words;
import android.tkb.util.word.Contants;
import android.tkb.util.word.PageUtil;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
/**
 * 英中互议界面类
 * @author Administrator
 *
 */
public class EnOrChTestActivity extends Activity{
	 private static final int NUM = 4;
	
	 private boolean isFirst = true;//是否是第一次标识符
	 private int flag = 0;
	 
	 private TextView word,textView2;
	 private Button but4Result,back;
	 private ListView lv;
	 
	 private List<Words> everyTopWordList = new ArrayList<Words>() ;//保存每次查出来的第一笔资料
	 private ArrayList<Map<String,Object>> data;
	 private List<Words> wordList;
	 private Cursor cur;
	 private Words w;
	 
	 private String value = Contants.E ;//给定一个初使值
	
	private Handler handler = new Handler();


	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.enorch_test);
		 
		 /* 从上一下物件接收内容++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
		 Intent intent = getIntent();
		 value = (String) intent.getCharSequenceExtra("key");//改变状态
		 
		 //初使控件
		 word = (TextView) findViewById(R.id.word);
		 lv = (ListView) findViewById(R.id.enorchTextLv);
		 but4Result = (Button) findViewById(R.id.testResult);
		 
		 but4Result.setOnClickListener(new But4ResultListener());//增加事件
		 back = (Button) findViewById(R.id.back);
			
		 back.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(EnOrChTestActivity.this, MainActivity.class);
				startActivity(intent);
				finish();
			}
			 
		 });
		 
		 if(Contants.E.equals(value)){
			 setTitle(R.string.word_test_en);//显示标题(英-->中)
		 }else{
			 setTitle(R.string.word_test_ch);
		 }
		 
		 hanldShowContent();//调用显示的内容
		 
	 }
	 
	 class But4ResultListener implements OnClickListener{
		 @Override
			public void onClick(View v) {
				if(isFirst && everyTopWordList.size()>0){
					
					everyTopWordList.remove(flag-1);
				}
				Intent intent  = new Intent();//到下一下一个页面－－－－－－－－－－－－－－－－－－－－－－－
				intent.setClass(EnOrChTestActivity.this, TestResultActivity.class);
				Words words3 = new Words();
				words3.setPrefeList(everyTopWordList);
				intent.putExtra("words", words3);
				intent.putExtra("flag", value);
				startActivity(intent);
				finish();
				
			}
	 }
	 

	 public void hanldShowContent(){
		 wordList = getRandomData(NUM+"");
		 
		 if(wordList.size() > 0){//保存第一笔资料
			 everyTopWordList.add(wordList.get(0));
		 }
		 
		 //对取到的笔数进行亂數處理
		 wordList = PageUtil.randdomSub(wordList,NUM);
		 
		 data = loadData(wordList);//组装ListView所需的数据
		 SimpleAdapter adapter = null;
		 
		 if(value.equals(Contants.E)){//英－－〉中
			 if(everyTopWordList.size() > flag){//显示某个单词
				 w = everyTopWordList.get(flag);
				 word.setText(w.getEn()) ;//显示英文
				 flag++;
			 }
			 
		     adapter  = new SimpleAdapter(this,data,android.R.layout.simple_list_item_1,new String[]{"jp"},new int[]{android.R.id.text1});
		    
		 }else if(value.equals(Contants.C)){//中－－〉英
			 if(everyTopWordList.size() > flag){//显示某个单词
				 w = everyTopWordList.get(flag);
				 word.setText(w.getJp()) ;//显示中文
				 flag++;
			 }
			 adapter  = new SimpleAdapter(this,data,android.R.layout.simple_list_item_1,new String[]{"en"},new int[]{android.R.id.text1});
		 }
		 lv.setAdapter(adapter);
	     lv.setOnItemClickListener(new ListViewOnItemClickListener());
	    
	 }
	 
	 /**
	  * 英－－〉中或中－－〉英的(共用)事件
	  * @author Administrator
	  *
	  */
	 class ListViewOnItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			Words selVal = wordList.get(position);
			
			textView2 = (TextView) findViewById(R.id.textView2);
		    if(isFirst){
		    	TextView tv= (TextView) arg1;
		    	
				tv.setTextColor(Color.RED);
				
				Words topWords = everyTopWordList.get(flag-1);
		    	if(selVal.getEn().equals(w.getEn())){//相等 
					textView2.setText(Contants.V);
					topWords.setTempState(Contants.V);
				}else{
					textView2.setText(Contants.X);
					textView2.setTextColor(Color.RED);
					topWords.setTempState(Contants.X);
				}
		    	isFirst = false;
		    	
			     //設定定時要執行的方法
			    handler.removeCallbacks(updateTimer);
			     //設定Delay的時間
			    handler.postDelayed(updateTimer, 2000);
			   
		    }
		    lv.setEnabled(false);
		}
	 
	 }
	 
     public ArrayList<Map<String,Object>> loadData( List<Words> wordList){
    	ArrayList<Map<String,Object>> coll = new ArrayList<Map<String,Object>>();
    	Map<String,Object> item ;
    
    	 for (int i = 0; i < wordList.size(); i++) {//显示单 词文本作答案用处
			 Words type = (Words) wordList.get(i);
			 item =  new HashMap<String,Object> ();
			 item.put("jp", type.getJp());
			 item.put("en", type.getEn());
			 coll.add(item);
    	 }
    	return coll;
    
    }
     
     //固定要執行的方法
     private Runnable updateTimer = new Runnable() {
         public void run() {
        	 isFirst = true;
			 textView2 = (TextView) findViewById(R.id.textView2);
			 textView2.setText("");//还原值
			 textView2.setTextColor(Color.WHITE);
			 lv.setEnabled(true);//可编辑状态
        	 hanldShowContent();
            
         }
     };

     
	 /**
	  * 取得随机几笔资料
	  * @return
	  */
	 public List<Words> getRandomData(String num){
		 DatabaseHelper mOpenHelper = new DatabaseHelper(EnOrChTestActivity.this);
		 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		 List<Words> resultList =new ArrayList<Words>();
		 cur = db.query("WORDS", new String[]{"EN","JP"}, null, null, null, null, "RANDOM()", num);			
		 while (cur.moveToNext()) {
			Words wd = new Words();
			 
			String en = cur.getString(cur.getColumnIndex("EN"));
			String ch = cur.getString(cur.getColumnIndex("JP"));
			wd.setEn(en);
			wd.setJp(ch);
			resultList.add(wd);
				
		 }
		 cur.close();
		 db.close();
		 mOpenHelper.close();
		 return resultList;
	 }
}
